//
//  UserInfoModel.m
//  XV
//
//  Created by apple on 2019/03/11.
//  Copyright © 2019 XV. All rights reserved.
//

#import "UserInfoModel.h"
#import "MJExtension.h"
#import "XSJTool.h"


@implementation UserInfoListModel

+ (NSDictionary *)mj_objectClassInArray
{
    return @{
             @"list" : @"UserInfoModel"
             };
}

@end


@implementation XRCookieModel

MJCodingImplementation


@end



@implementation UserInfoModel

//userDefaults
#define kUserInfo @"UserInfo"

MJCodingImplementation


+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"ID":@"id"
             };
}

/**
 *  当字典转模型完毕时调用
 */
- (void)mj_didConvertToObjectWithKeyValues
{
    
}




//获取性别名称
-(NSString*)getSexText
{
    if ([self.gender isEqualToString:@"f"])
    {
        return @"男";
    }
    
    return @"女";
}

-(NSInteger)getSexIndex
{
    if ([self.gender isEqualToString:@"f"])
    {
        return 0;
    }
    
    return 1;
}

//获取用户信息
+(UserInfoModel*)readUserInfo
{
    // 解档
    NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:kUserInfo];
    UserInfoModel *userInfo = nil;
    //crash处理
    if (userInfoData)
    {
        @try
        {
            userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
        }
        @catch (NSException *exception)
        {
            return userInfo;
        }
        @finally
        {
            
        }
    }
    
    return userInfo;
}


//保存用户信息
-(void)saveUserInfo
{
    // 归档
    NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:self];
    [[NSUserDefaults standardUserDefaults] setObject:userInfoData forKey:kUserInfo];
}

//删除用户信息
+(void)removeUserInfo
{
    // 归档
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kUserInfo];
}


//获取UID
+(NSString*)getUID
{
    UserInfoModel* u = [UserInfoModel readUserInfo];
    return u.userId;
}

//当前用户是自己
+(BOOL)isSelf:(NSString*)uid
{
    if (kStringIsEmpty(uid))
    {
        return NO;
    }
    
    return [uid isEqualToString:[UserInfoModel getUID] ];
}

//是否审核状态
+(BOOL)isReviewStatus
{
    return NO;
}


//登录类型 0:验证码登录 1:账号登录 2:微信登录 3:QQ登录
+(void)setLoginType:(NSInteger)loginType
{
    [[NSUserDefaults standardUserDefaults] setInteger:loginType forKey:@"loginType"];
    //同步
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSInteger)getLoginType
{
    NSString* str = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginType"];
    return [str integerValue];
}


@end
